package GUI;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JButton;

import Controller.DBcontroller;
import Entity.Complaint;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;

/**
 * this is the GUI for customer handeling view. the service class worker can view former complaints by this GUI. 
 * @author Moran Abilea
 */
public class CustomerHandelingViewBoundary extends JFrame {
	JFrame frame;
	JPanel lastPanel;
	public void setLastPanel (JPanel panel)
	{
		this.lastPanel=panel;
	}
	public void setFrame (JFrame frame)
	{
		this.frame=frame;
	}

	private JPanel contentPane;
	private JTable table;

	/**
	 * is the constructor. it has only one table for viewing the complaints and button for going back.
	 */
	public CustomerHandelingViewBoundary(Complaint[]wc) {
		setTitle("Customer Handeling View Window");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 825, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblCustomerComplaintHandeling = new JLabel("Customer Complaint Handeling View ");
		lblCustomerComplaintHandeling.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblCustomerComplaintHandeling.setBounds(289, 11, 231, 22);
		contentPane.add(lblCustomerComplaintHandeling);
		
		JLabel lblWorkerIdauto = new JLabel("Worker id: "+DBcontroller.getLoggedIn());
		lblWorkerIdauto.setBounds(50, 42, 164, 14);
		contentPane.add(lblWorkerIdauto);
		
		// don't forget to run a SQL query in order to make the JTable visible.
		DBcontroller.viewWorkerComplaintSQL(DBcontroller.getLoggedIn());
		
		JLabel lblHandledComplaints = new JLabel("Handled Complaints");
		lblHandledComplaints.setBounds(345, 62, 118, 14);
		contentPane.add(lblHandledComplaints);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(50, 87, 738, 135);
		contentPane.add(scrollPane);
		
		table = new JTable(new viewhandleJTable(wc));
		scrollPane.setViewportView(table);
		
		JButton btnGoBack = new JButton("Go back");
		btnGoBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close();
				frame.setContentPane(lastPanel);
				frame.setVisible(true);
				
			}
		});
		btnGoBack.setBounds(50, 228, 139, 23);
		contentPane.add(btnGoBack);
	}
	
	public void close()
	{
		WindowEvent windowclose=new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
		Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(windowclose);
	}
}

class viewhandleJTable extends AbstractTableModel
{
	String[] titles={"Complaint Num","Customer ID","Submitssion Date","Description","Answer Date","Answer","compenstation"};
	Object[][] data;
	
	public viewhandleJTable(Complaint[] c)
	{
		super();
		this.data=new Object[c.length][7];
		for(int i=0; i<c.length; i++)
		{
			this.data[i][0]=c[i].getComplaint_number();
			this.data[i][1]=c[i].getCustomer_id();
			this.data[i][2]=c[i].getDateOfSubmition();
			this.data[i][3]=c[i].getDescription();
			this.data[i][5]=c[i].getResult();
			this.data[i][4]=c[i].getDateOfAnswer();
			this.data[i][6]=c[i].getCompensation();
		}
	}
	// override the other function.
	@Override
	public int getColumnCount() {return this.titles.length;}

	@Override
	public int getRowCount() {return data.length;}

	@Override
	public Object getValueAt(int arg0, int arg1) {return data[arg0][arg1];}
	
	@Override
	public String getColumnName(int column) {return (String) this.titles[column];}
}